package com.shengsiyuan.netty.buf;

import java.util.Iterator;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.CompositeByteBuf;
import io.netty.buffer.Unpooled;

public class CompositeByteBufTest {
    public static void main(String[] args) {
        CompositeByteBuf compositeByteBuf = Unpooled.compositeBuffer();
        ByteBuf heathBuf = Unpooled.buffer(10);
        ByteBuf directBuf = Unpooled.directBuffer(8);

        compositeByteBuf.addComponents(heathBuf, directBuf);
        Iterator<ByteBuf> iter = compositeByteBuf.iterator();
        while (iter.hasNext()) {
            System.out.println(iter.next());

        }
    }

    // 创建包含指定字符串的ByteBuf，在获取buf内容时，需要判断是否有hasArray
    public void testHasArrayByteBuf() {
        ByteBuf buf = Unpooled.copiedBuffer("章Hello word", Charset.forName("utf-8"));
        //需要判断buf是堆内空间还是堆外空间，如果是对外空间（直接内存）是没法获取到内部字节数组的
        if (buf.hasArray()) {
            byte[] content = buf.array();
            System.out.println(new String(content, Charset.forName("utf-8")));
        }
    }

    //使用Nio channel读取文件
    public void testFileByteBuf() throws FileNotFoundException {
        FileInputStream fileInputStream = new FileInputStream("文件.txt");
        FileChannel channel = fileInputStream.getChannel();

    }


}
